<template>
  <el-drawer
    :visible.sync="drawerVisible"
    title="加入专辑"
    size="60%"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="beforeClose"
    custom-class="tag-drawer join-album-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <album-list ref="albumList" selection></album-list>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="beforeClose()">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
import albumList from './list'
export default {
  components: {
    albumList
  },
  props: {
    assetId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      drawerVisible: false,
      belongAlbumIds: []
    }
  },
  methods: {
    showDrawer() {
      this.drawerVisible = true
      this.getAlbumId()
    },
    getAlbumId() {
      this.$ajax({
        url: this.$ajax.setUrl(`ast/astAlbumDetails/getAlbum/${this.assetId}`),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.belongAlbumIds = data.data
          this.$refs.albumList.getList(this.belongAlbumIds)
        } else {
          // 失败后执行的操作
        }
      })
    },
    submit() {
      let selectedRow = this.$refs.albumList.$refs.table.getSelectList()
      let albumIds = []
      selectedRow.forEach(v => albumIds.push(v.assetAlbumId))
      this.$ajax({
        url: this.$ajax.setUrl('ast/astAlbumDetails/addToAlbum'),
        method: 'post',
        data: this.$ajax.setData({
          assetId: this.assetId,
          assetAlbumId: albumIds
        })
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.$message.success(data.msg)
          this.beforeClose()
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    beforeClose() {
      this.belongAlbumIds = []
      this.$refs.albumList.clearSelection()
      this.drawerVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
